package com.offer;

import com.bean.ListNode;

import java.util.Stack;

/**
 * @Author: huangzhigao
 * @Date: 2023/3/4 15:51
 */
public class offer06 {
    public int[] reversePrint(ListNode head) {
        Stack<Integer> stack = new Stack<>();
        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }

        int[] res = new int[stack.size()];
        int i = 0;
        while (!stack.isEmpty()) {
            res[i++] = stack.pop();
        }
        return res;
    }


    public int[] reversePrint1(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
            count++;
        }
        int[] res = new int[count];
        int index = 0;
        while (pre != null) {
            res[index++] = pre.val;
            pre = pre.next;
        }
        return res;
    }
}
